{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<a name=\"top\"></a><img src=\"./source/SpinalHDL.png\" alt=\"SpinalHDL based on Scala\" style=\"width:320px;\" />"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "val path = System.getProperty(\"user.dir\") + \"/source/load-spinal.sc\"\n",
    "interp.load.module(ammonite.ops.Path(java.nio.file.FileSystems.getDefault().getPath(path)))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Pattern matching\n",
    "\n",
    "- Normal case pattern \n",
    "```scala\n",
    "x match {\n",
    "    case xx => ... \n",
    "    case zz => ...\n",
    "    case _  => ...\n",
    "}\n",
    "```\n",
    "- class Type self match \n",
    "```scala\n",
    "class C { self => }      // Give this an alias of self， this.xxx equal self.xxx \n",
    "class C { this:X => }    // Subclass of C must be mixed with X Trait \n",
    "class D extends C with X\n",
    "```\n",
    "- Seq-iterator Matching \n",
    "```scala\n",
    "List(\"a\",\"b\",\"c\",\"d\").map(x =>..)\n",
    "List(\"a\",\"b\",\"c\",\"d\").map(_)\n",
    "List(\"a\",\"b\",\"c\",\"d\").zipWithIndex.map((x,i)=>...)  \n",
    "List(\"a\",\"b\",\"c\",\"d\").zipWithIndex.map(_ _) \n",
    "```\n",
    "- Assignment matching \n",
    "```scala\n",
    "val (a,b) = (1,2)\n",
    "val Students(Name,Age) = Students(\"jack\", 18)\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "trait X\n",
    "class C { this:X => }\n",
    "class E extends C with X"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "val (a,b) = (1,2)\n",
    "case class Students(name:String, age:Int)\n",
    "val Students(name0,age0) = Students(\"jack\", 18)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Ubiquitous pattern matching\n",
    "```scala\n",
    "object BusInterface {\n",
    "  def apply(bus: Apb3, sizeMap: SizeMapping, selID: Int): BusIf = Apb3BusInterface(bus, sizeMap, selID)(moduleName)\n",
    "  def apply(bus: Apb3, sizeMap: SizeMapping, selID: Int, readSync: Boolean): BusIf = Apb3BusInterface(bus, sizeMap, selID, readSync)(moduleName)\n",
    "\n",
    "  def apply(bus: AhbLite3, sizeMap: SizeMapping): BusIf = AhbLite3BusInterface(bus, sizeMap)\n",
    "  def apply(bus: AhbLite3, sizeMap: SizeMapping, readSync: Boolean): BusIf = AhbLite3BusInterface(bus, sizeMap, readSync)\n",
    "\n",
    "  def apply(bus: Axi4, sizeMap: SizeMapping): BusIf = Axi4BusInterface(bus, sizeMap)\n",
    "  def apply(bus: Axi4, sizeMap: SizeMapping, readSync: Boolean): BusIf = Axi4BusInterface(bus, sizeMap)\n",
    "\n",
    "  def apply(bus: AxiLite4, sizeMap: SizeMapping): BusIf = AxiLite4BusInterface(bus, sizeMap)\n",
    "  def apply(bus: AxiLite4, sizeMap: SizeMapping, readSync: Boolean): BusIf = AxiLite4BusInterface(bus, sizeMap)\n",
    "}\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Scala",
   "language": "scala",
   "name": "scala"
  },
  "language_info": {
   "codemirror_mode": "text/x-scala",
   "file_extension": ".scala",
   "mimetype": "text/x-scala",
   "name": "scala",
   "nbconvert_exporter": "script",
   "version": "2.12.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
